//获取节点
const loginNameEle = document.querySelector('input[name="username"]')
const loginPasswordEle = document.querySelector('input[name="password"]')
const loginIdText = document.querySelector('.loginIdText')
const loginPasswordText = document.querySelector('.loginPasswordText')
const loginBtn = document.querySelector('.loginBtn')

//账号非空验证
const emptyChecjedId = () => {
    //获取节点内容
    let loginId = loginNameEle.value
    if (loginId == '') {
        loginIdText.innerHTML = '用户名不能为空！'
        return false
    } else {
        loginIdText.innerHTML = ''
        return true
    }

}
//密码非空验证
const emptyChecjedPassword = () => {
    let loginPassword = loginPasswordEle.value
    if (loginPassword == '') {
        loginPasswordText.innerHTML = '密码不能为空！'
        return false
    } else {
        loginPasswordText.innerHTML = ''
        return true

    }
}

//点击事件
loginBtn.onclick = async function () {
    let isEmptyChecjedId = emptyChecjedId()
    let isEmptyChecjedPassword = emptyChecjedPassword()
    if (!isEmptyChecjedId || !isEmptyChecjedPassword) {
        return
    }

    let data = await ajaxPromise({
        method: "post",
        url: 'http://10.7.171.143:8088/api/login',
        data: {
            username: loginNameEle.value,
            password: loginPasswordEle.value
        }
    })
    if (data.resultCode == -1) {
        alert('用户名或密码错误！')
    } else {
        //保存用户信息到localstorage
        let user = {
            headerimg: data.resultInfo.headerimg,
            nick: data.resultInfo.nick
        }
        localStorage.setItem('USER', JSON.stringify(user))
        location.href = '../后台管理/index.html'
        alert('登录成功！')
    }

}

//焦点事件
loginNameEle.addEventListener('blur', function () {
    let isEmptyChecjedId = emptyChecjedId()
    if (!isEmptyChecjedId) {
        return
    }
})
//密码焦点事件
loginPasswordEle.addEventListener('blur', function () {
    let isEmptyChecjedPassword = emptyChecjedPassword()
    //如果为空，则不进行强度验证，不为空时，再进行强度验证
    if (!isEmptyChecjedPassword) {
        return
    }
})


//点击注册跳转到注册界面
// function toRegister() {
//     const toRegisterEle = document.querySelector('.registerBtn')
//     toRegisterEle.onclick = function () {
//         location.href = './register.html'
//     }
// }
// toRegister()